extends Node2D

class_name UnitAI

signal order_done
signal ai_init
signal request_path

export (bool) var friendly = false

onready var unit := get_parent()
onready var speed

var PathLine = Line2D.new()

var path := PoolVector2Array()

func _ready():
	PathLine.default_color = Color(randf(), randf(), randf())
	PathLine.width = 1
	PathLine.joint_mode = Line2D.LINE_JOINT_ROUND
	get_tree().root.get_child(1).add_child(PathLine)

func request_path(dest: Vector2):
	emit_signal("request_path", self, dest)

func _physics_process(delta):
	get_moving(delta)

func get_moving(delta):
	if path.size() != 0:
		var dist = unit.position.distance_to(path[0])
		if not unit.is_within(dist, 0, 2):
			unit.direction = unit.position.direction_to(path[0])
		elif path.size() == 1:
				emit_signal("order_done")
		else:
			path.remove(0)
			PathLine.points = path
